package cn.zlj.pethome.pet.controller;

import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.query.BaseQuery;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.basic.util.LoginContext;
import cn.zlj.pethome.org.domain.TEmployee;
import cn.zlj.pethome.org.mapper.EmployeeMapper;
import cn.zlj.pethome.org.service.EmployeeService;
import cn.zlj.pethome.pet.domain.PetDetail;
import cn.zlj.pethome.pet.query.PetQuery;
import cn.zlj.pethome.pet.service.IPetService;
import cn.zlj.pethome.pet.domain.Pet;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @Autowired
    private EmployeeService employeeMapper;


    @PostMapping("/offsale")
    public AjaxResult toffsale(@RequestBody List<Long> ids) {
      petService.toffsale(ids);
      return AjaxResult.me().success();
    }
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        petService.onsale(ids);
        return AjaxResult.me().success();
    }

    /**
     * 保存和修改公用的
     *
     * @param pet 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        try {
            if (pet.getId() != null){
                petService.update(pet);
            }
            else{
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Long id = logininfo.getId();
                TEmployee employee = employeeMapper.selectByLogininfoId(id);
                pet.setState(0);
                pet.setUserId(employee.getId());
                pet.setShopId(employee.getShopId());
                petService.add(pet);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id") Long id) {
        return petService.findById(id);
    }

    @GetMapping("/detail/{id}")
    public PetDetail getdetail(@PathVariable("id") Long id) {
        return petService.findDetailById(id);
    }
    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Pet> list() {

        return petService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public QueryResult<Pet> json(@RequestBody PetQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        System.out.println(logininfo);
        return petService.queryByPage(query);
    }
    @GetMapping("/getPetDetail/{id}")
    public Pet getPetDetail(@PathVariable("id") Long id) throws TemplateException, IOException {
        return petService.getPetDetail(id);
    }
}
